package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/6/10 16:05
 * https://leetcode.cn/problems/sort-colors/
 */
public class Title75 {

    public void sortColors(int[] nums) {
        int left = 0, right = nums.length - 1, cur = 0;
        while (cur <= right) {
            if (nums[cur] == 0 && cur != 0) {
                swap(nums, cur, left);
                left++;
                cur++;
            } else if (nums[cur] == 2 && cur != nums.length - 1) {
                swap(nums, cur, right);
                cur++;
                right--;
            }else{
                cur++;
            }
        }
    }

    private void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    public void sortColors2(int[] nums) {
        int left = 0, right = nums.length - 1, cur = 0;
        while (cur <= right) {
            if (nums[cur] == 2) {
                swap(nums, cur, right);
                right--;
            } else if (nums[cur] == 0) {
                swap(nums, left, cur);
                left++;
                cur++;
            }else{
                cur++;
            }
        }
    }

    public static void main(String[] args) {

    }
}
